
local i 0

* 2002 - 2009 
forv year = 2002(1)2009 { 

import delimit using ../Data/Raw/OES/nat4d_M`year'_dl.csv, delim(",") clear
cap rename ïnaics naics 
tostring naics, replace
drop if tot_emp=="**"
drop if h_mean=="*"|h_mean=="#"
drop if a_mean=="*"|a_mean=="#"

replace tot_emp = subinstr(tot_emp, ",", "",.)
destring tot_emp, replace  
destring h_mean, replace
destring a_mean, replace

local ++i 
local year_`i' `year'

* Employment in RPO (employment services industry)
sum tot_emp if naics=="561300" & occ_title=="Industry Total"
local emp_rpo_`i' = r(mean)

* Recruiter employment 
sum tot_emp if occ_code=="13-1071"
local emp_recruiters_`i' = r(mean)*r(N)

* Recruiters in RPO
sum tot_emp if naics=="561300" & occ_code=="13-1071"
local emp_recruitersinrpo_`i' = r(mean)

* Recruiters in RPO (hourly wage)
sum h_mean if naics=="561300" & occ_code=="13-1071"
local wageh_recruitersinrpo_`i' = r(mean)

* Recruiters in RPO (annual wage)
sum a_mean if naics=="561300" & occ_code=="13-1071"
local wagea_recruitersinrpo_`i' = r(mean)


* Total employment
sum tot_emp if occ_title=="Industry Total"
local emp_total_`i' = r(mean)*r(N)

sum a_mean if occ_title=="Industry Total"
local wagea_total_`i' = r(mean)

sum h_mean if occ_title=="Industry Total"
local wageh_total_`i' = r(mean)


dis `emp_rpo_`i''
dis `emp_recruiters_`i''
dis `emp_recruitersinrpo_`i''
dis `emp_total_`i''

}




* 2010, 2011: 13-1071 changed to 13-1078 (which includes labor relations specialists

forv year = 2010(1)2011 { 

import delimit using ../Data/Raw/OES/nat4d_M`year'_dl.csv, delim(",") clear
cap rename ïnaics naics 
tostring naics, replace
drop if tot_emp=="**"
drop if h_mean=="*"|h_mean=="#"
drop if a_mean=="*"|a_mean=="#"

replace tot_emp = subinstr(tot_emp, ",", "",.)
replace a_mean = subinstr(a_mean, ",", "",.)
destring tot_emp, replace  
destring h_mean, replace
destring a_mean, replace


local ++i 
local year_`i' `year'

* Employment in RPO (employment services industry)
sum tot_emp if naics=="561300" & occ_title=="Industry Total"
local emp_rpo_`i' = r(mean)

* Recruiter employment 
sum tot_emp if occ_code=="13-1078"
local emp_recruiters_`i' = r(mean)*r(N)

* Recruiters in RPO
sum tot_emp if naics=="561300" & occ_code=="13-1078"
local emp_recruitersinrpo_`i' = r(mean)


* Recruiters in RPO (hourly wage)
sum h_mean if naics=="561300" & occ_code=="13-1078"
local wageh_recruitersinrpo_`i' = r(mean)

* Recruiters in RPO (annual wage)
sum a_mean if naics=="561300" & occ_code=="13-1078"
local wagea_recruitersinrpo_`i' = r(mean)


* Total employment
sum tot_emp if occ_title=="Industry Total"
local emp_total_`i' = r(mean)*r(N)

dis `emp_rpo_`i''
dis `emp_recruiters_`i''
dis `emp_recruitersinrpo_`i''
dis `emp_total_`i''

}



* 2012, 2013: two files
forv year = 2012(1)2013 { 

* File 1
import delimit using ../Data/Raw/OES/nat4d_M`year'_dl_1.csv, delim(",") clear

tempfile one
save `one'

* File 2 
import delimit using ../Data/Raw/OES/nat4d_M`year'_dl_2.csv, delim(",") clear

append using "`one'"

cap rename ïnaics naics 
tostring naics, replace
drop if tot_emp=="**"
drop if h_mean=="*"|h_mean=="#"
drop if a_mean=="*"|a_mean=="#"

replace tot_emp = subinstr(tot_emp, ",", "",.)
replace a_mean = subinstr(a_mean, ",", "",.)
destring tot_emp, replace  
destring h_mean, replace
destring a_mean, replace

keep if inlist(occ_group, "detailed", "total")
 
local ++i 
local year_`i' `year'

* Employment in RPO (employment services industry)
sum tot_emp if naics=="561300" & occ_title=="Industry Total"
local emp_rpo_`i' = r(mean)

* Recruiter employment 
sum tot_emp if occ_code=="13-1071"
local emp_recruiters_`i' = r(mean)*r(N)

* Recruiters in RPO
sum tot_emp if naics=="561300" & occ_code=="13-1071"
local emp_recruitersinrpo_`i' = r(mean)

* Recruiters in RPO (hourly wage)
sum h_mean if naics=="561300" & occ_code=="13-1071"
local wageh_recruitersinrpo_`i' = r(mean)

* Recruiters in RPO (annual wage)
sum a_mean if naics=="561300" & occ_code=="13-1071"
local wagea_recruitersinrpo_`i' = r(mean)


* Total employment
sum tot_emp if occ_title=="Industry Total"
local emp_total_`i' = r(mean)*r(N)

dis `emp_rpo_`i''
dis `emp_recruiters_`i''
dis `emp_recruitersinrpo_`i''
dis `emp_total_`i''

}




* 2014 -  2019 
forv year = 2014(1)2019  { 


import delimit using ../Data/Raw/OES/nat4d_M`year'_dl.csv, delim(",") clear varnames(1)

cap rename ïnaics naics 
tostring naics, replace
drop if tot_emp=="**"
drop if h_mean=="*"|h_mean=="#"
drop if a_mean=="*"|a_mean=="#"

replace tot_emp = subinstr(tot_emp, ",", "",.)
replace a_mean = subinstr(a_mean, ",", "",.)
destring tot_emp, replace  
destring h_mean, replace
destring a_mean, replace

replace occ_title="Industry Total" if occ_title=="All Occupations" //for 2019 only

local ++i 
local year_`i' `year'

* Employment in RPO (employment services industry)
sum tot_emp if naics=="561300" & occ_title=="Industry Total"
local emp_rpo_`i' = r(mean)

* Recruiter employment 
sum tot_emp if occ_code=="13-1071"
local emp_recruiters_`i' = r(mean)*r(N)

* Recruiters in RPO
sum tot_emp if naics=="561300" & occ_code=="13-1071"
local emp_recruitersinrpo_`i' = r(mean)

* Recruiters in RPO (hourly wage)
sum h_mean if naics=="561300" & occ_code=="13-1071"
local wageh_recruitersinrpo_`i' = r(mean)

* Recruiters in RPO (annual wage)
sum a_mean if naics=="561300" & occ_code=="13-1071"
local wagea_recruitersinrpo_`i' = r(mean)

* Total employment
sum tot_emp if occ_title=="Industry Total"
local emp_total_`i' = r(mean)*r(N)

sum a_mean if occ_title=="Industry Total"
local wagea_total_`i' = r(mean)

sum h_mean if occ_title=="Industry Total"
local wageh_total_`i' = r(mean)


dis `emp_rpo_`i''
dis `emp_recruiters_`i''
dis `emp_recruitersinrpo_`i''
dis `emp_total_`i''

}


local I `i'
clear
set obs `I'
foreach x in year emp_rpo emp_recruiters emp_recruitersinrpo wageh_recruitersinrpo wagea_recruitersinrpo emp_total wagea_total wageh_total {
	qui gen `x' = ""
	forv i = 1/`I' {
		qui replace `x' = "``x'_`i''" in `i'
	}
	destring `x', replace
}


* rescale to 2002
foreach y in 2002 { 
	foreach x in  emp_recruitersinrpo  wageh_recruitersinrpo wagea_recruitersinrpo emp_total wagea_total wageh_total {
		* Scale to 2002
		qui sum `x' if year==`y'
		qui gen `x'_`y' =`x'/r(mean)*100
		* % change from 2002
		gen pct_`x' = (`x'-`x'_`y')/`x'_`y'*100
	}
}


********************************************************************
* Figure 1: Growth in recruiters in employment services, 2002-2019 *
********************************************************************


twoway (connected  emp_recruitersinrpo_2002 year, msymbol(diamond) lcolor(black) mcolor(black)) (connected emp_total_2002 year, lcolor(gs12) mcolor(gs12) msymbol(circle)), graphregion(color(white)) bgcolor(white) xlabel(2002(4)2019) legend( rows(1) label(1 "Recruiters in employment services") label( 2 "Total US employment")) ytitle("Employment growth (2002=100)")
graph export "../Output/Figures/Figure01.pdf", replace




******************************************************
* Table A1: GROWTH IN OUTSOURCED RECRUITING, PANEL A *
******************************************************


sum emp_recruitersinrpo_2002 wageh_recruitersinrpo_2002 wagea_recruitersinrpo_2002 if year==2019
sum emp_total_2002 wagea_total_2002 wageh_total_2002 if year==2019

